/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonyodev.fetch2.database;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.StoreConfig;
import ohos.data.resultset.ResultSet;
import ohos.data.resultset.ResultSetHook;

import com.tonyodev.fetch2.database.migration.Migration;

import java.util.ArrayList;
import java.util.List;

/**
 * @since 2021-05-26
 */
public class DownloadDatabase_Impl extends DownloadDatabase {
    private String TAG = "lpq " + DownloadDatabase_Impl.class.getSimpleName();
    private volatile DownloadDao _downloadDao;
    private RdbStore rdbStore;

    /**
     * DownloadDatabase_Impl之构造函数
     *
     * @param context 上下文
     * @param namespace 数据库文件名
     * @param migrations 数据库版本迁移，暂无此高级逻辑数据
     */
    public DownloadDatabase_Impl(Context context, String namespace, List<Migration> migrations) {
        super(context);
        init(namespace, migrations);
    }

    private void init(String namespace, List<Migration> migrations) {
        StoreConfig config = StoreConfig.newDefaultConfig(namespace + ".db");
        int version = 1;
        if (migrations != null && migrations.size() > 0) {
            for (Migration info : migrations) {
                if (version < info.endVersion) {
                    version = info.endVersion;
                }
            }
        }
        rdbStore = getRdbStore(config, version, new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
                System.out.println(TAG + " onCreate ");
                //创建数据表
                rdbStore.executeSql("create table if not exists " + DownloadDatabase.TABLE_NAME + "(" + DownloadDatabase.COLUMN_ID
                    + " integer primary key autoincrement," + DownloadDatabase.COLUMN_NAMESPACE + " text,"
                    + DownloadDatabase.COLUMN_URL + " text,"
                    + DownloadDatabase.COLUMN_FILE + " text,"
                    + DownloadDatabase.COLUMN_GROUP + " integer,"
                    + DownloadDatabase.COLUMN_PRIORITY + " integer,"
                    + DownloadDatabase.COLUMN_HEADERS + " text,"
                    + DownloadDatabase.COLUMN_DOWNLOADED + " long,"
                    + DownloadDatabase.COLUMN_TOTAL + " long,"
                    + DownloadDatabase.COLUMN_STATUS + " integer,"
                    + DownloadDatabase.COLUMN_ERROR + " integer,"
                    + DownloadDatabase.COLUMN_NETWORK_TYPE + " integer,"
                    + DownloadDatabase.COLUMN_CREATED + " long,"
                    + DownloadDatabase.COLUMN_TAG + " text,"
                    + DownloadDatabase.COLUMN_ENQUEUE_ACTION + " integer,"
                    + DownloadDatabase.COLUMN_IDENTIFIER + " long,"
                    + DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE + " integer,"
                    + DownloadDatabase.COLUMN_EXTRAS + " text,"
                    + DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS + " integer,"
                    + DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS + " integer"
                    + ")");

//                for (Migration info : migrations) {
//                    info.migrate(rdbStore);
//                    System.out.println(TAG + " onCreate migrate ");
//                }

                System.out.println(TAG + " onCreate end ");
            }

            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {


            }
        }, new ResultSetHook() {
            @Override
            public void createHook(String s, String[] strings, ResultSet resultSet) {

            }
        });

    }

    public RdbStore getRdbStore() {
        return rdbStore;
    }


    public DownloadDao requestDao() {
        if (_downloadDao != null) {
            return _downloadDao;
        } else {
            synchronized (this) {
                if (_downloadDao == null) {
                    _downloadDao = new DownloadDao_Impl(rdbStore);
                }
                return _downloadDao;
            }
        }
    }

}
